package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqut.service.IComParamService;
import com.cqut.model.CodeManager;
import com.cqut.model.ComParam;
import com.cqut.util.BeanUtil;
import com.cqut.util.SqlUtil;
import com.cqut.util.StringUtil;
import com.cqut.dao.ComParamMapper;
import com.cqut.dao.ComParamMapperExtra;
import com.cqut.dto.CodeManagerDTO;
import com.cqut.dto.ComParamDTO;
import com.cqut.dto.LimitShowDTO;

@Service
public class ComParamService implements IComParamService {
    @Resource(name="comParamMapper")
    private ComParamMapper comParamMapper;
    @Resource(name="comParamMapperExtra")
    private ComParamMapperExtra comParamMapperExtra;
    
    @Override
    public boolean save(ComParam comParam) {
        return comParamMapper.insert(comParam) > 0;
    }

    @Override
    public boolean update(ComParam comParam) {
        return comParamMapper.updateByPrimaryKeySelective(comParam) >= 0;
    }

    @Override
    public boolean delete(String comParamId) {
        return comParamMapper.deleteByPrimaryKey(comParamId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return comParamMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public ComParam findModel(String comParamId) {
        return comParamMapper.selectByPrimaryKey(comParamId);
    }

    @Override
    public ComParam findModel(String comParamId, String[] parameters) {
        return comParamMapper.findModel(comParamId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String comParamId) {
        String[][] parameters = {{"com_param_id", "comParamId"}, {"com_param_no", "comParamNo"}, {"com_param_type", "comParamType"}, {"com_param_name", "comParamName"}, {"com_param_level", "comParamLevel"}, {"parent_com_param_no", "parentComParamNo"}, {"user_id", "userId"}};
        return this.findMap(comParamId, parameters);
    }

    public Map<String, Object> findMap(String comParamId, String[][] parameters) {
        return comParamMapper.findMap(comParamId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<ComParam> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<ComParam> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<ComParam> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return comParamMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"com_param_id", "comParamId"}, {"com_param_no", "comParamNo"}, {"com_param_type", "comParamType"}, {"com_param_name", "comParamName"}, {"com_param_level", "comParamLevel"}, {"parent_com_param_no", "parentComParamNo"}, {"user_id", "userId"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"com_param_id", "comParamId"}, {"com_param_no", "comParamNo"}, {"com_param_type", "comParamType"}, {"com_param_name", "comParamName"}, {"com_param_level", "comParamLevel"}, {"parent_com_param_no", "parentComParamNo"}, {"user_id", "userId"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return comParamMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return comParamMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

    @Override
	@Transactional
	public boolean addTableComParam(ComParamDTO comParamDTO) {
		ComParam tableComParam = comParamDTO.toModel();
		String parentCode=tableComParam.getParentComParamNo();
		if(StringUtil.isNullOrEmpty(parentCode)){
			parentCode=null;
		}
		tableComParam.setComParamId(creatTreeParamCode(parentCode));
		tableComParam.setComParamLevel(creatLevel0(parentCode));
		
		if(comParamMapper.insertSelective(tableComParam)<=0)
			return false;
		return true;
	}
	
	private String creatTreeParamCode(String parentCode) {
		Map<String, Object> maxCode = comParamMapperExtra.findMaxComParamId(parentCode);
		
		if (maxCode == null)
			return BeanUtil.getTreeCodeForInit(parentCode);
		      
		return BeanUtil.getTreeCode(maxCode.get("maxCode").toString());
	}
	
	private int creatLevel0(String parentCode) {
		ComParam tableCode = comParamMapper.selectByPrimaryKey(parentCode);
		
		if (tableCode == null)
			return 1;
		
		return tableCode.getComParamLevel() + 1;
	}
    
    
	@Override
	public boolean duplicateCheckingComParamName(String comParamName) {
		// TODO Auto-generated method stub
		return comParamMapperExtra.duplicateCheckingComParamName(comParamName)!=null;
	}

	@Override
	public boolean CheckingComParamType(String comParamType) {
		// TODO Auto-generated method stub
		return comParamMapperExtra.CheckComParamType(comParamType)>0;
	}
	
	@Override
	public List<Map<String, Object>> findChildrenByComParamType(String comParamType) {
		// TODO Auto-generated method stub
		return comParamMapperExtra.getComParamType(comParamType);
	}

	

	@Override
	public boolean deleteTableComParamsByParamComParams(String[] comParamNoArray) {
		// TODO Auto-generated method stub
		if(comParamMapperExtra.deleteByLikeParamComParam(comParamNoArray)<=0)
			return false;
		return true;
	}

	@Override
	public List<Map<String, Object>> getComParamList(String comParamName, String parentId, LimitShowDTO limitShowDTO) {
		// TODO Auto-generated method stub
		return comParamMapperExtra.getListByComParamName(comParamName, parentId, limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
	}
	
	@Override
	public long getComParamListCount(String parentId, String comParamName) {
		// TODO Auto-generated method stub
		return comParamMapperExtra.getListCountByComParamName(parentId, comParamName);
	}
	
	@Override
	public Map<String, Object> getDetailById(String comParamId) {
		// TODO Auto-generated method stub
		return comParamMapperExtra.getDetailById(comParamId);
	}


	public Map<String,Object> ResponseEntity(ComParamDTO comParamDTO){
		return comParamMapperExtra.updateComParam(comParamDTO.getComParamId(),comParamDTO.getComParamName());
	}
	

	

}
